var app = getApp()
var util = require('../../md5.js');
Page({

  data: {
  
    userTelephone: '',
    name:'',
    useraddress:'',
    num: 1,
    needPay: 550,
    minusStatus: 'disabled' ,

  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    var needPay = this.data.needPay;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      needPay: 550*num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    var needPay = this.data.needPay;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      needPay: 550 * num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    var needPay = this.data.needPay;
    // 将数值与状态写回  
    this.setData({
      num: num,
     needPay: 550 * num
    });
  } , 
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({

      url: '/pages/index/index',

    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })

  },
  onLoad: function(options) {
    console.log(app.globalData.myId)
  },
  send: function () {
    if (this.data.name == '' || this.data.userTelephone == '' || this.data.useraddress == '') {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '请稍候',
    })
    
    var that = this
    var num = this.data.num;

    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/BlackBook/placeOrder',
      data: {
      
        name: this.data.name,
        mobile: this.data.userTelephone,
        number: num,
        address: this.data.useraddress
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        console.log(res.data)
        if (res.data.status === 1) {
          that.getPrePayId(res.data.oid)

        } else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }

      },
      fail: function () {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }


    })
  },

  getPrePayId: function (oid) {
    console.log(oid)
    console.log(app.globalData.myId)
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/WeiPayBook/AppPrePayProcess',
      data: {
        type:1,
        oid: oid,
        session3rd: app.globalData.myId,
      },
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
      
        if (res.data.status === 1) {
          console.log(res.data.nonce_str)
          console.log(res.data.prepay_id)
          that.sendPayMent(res.data.nonce_str, res.data.prepay_id)
        }
        else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }
      }, fail: function (e) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }

    })
  },

  sendPayMent: function (nonce_str, prepay_id) {
    var that = this
    var time = Date.parse(new Date()) / 1000;
    var paySign = util.hexMD5('appId=wxd59c11f9decb5e58&nonceStr=' + nonce_str + '&package=prepay_id=' + prepay_id + '&signType=MD5&timeStamp=' + time + '&key=lkjmkluhjjm3256jkk52188iojkklok5')

    wx.requestPayment({
      'timeStamp': time + '',
      'nonceStr': nonce_str,
      'package': 'prepay_id=' + prepay_id,
      'signType': 'MD5',
      'trade_type': 'JSAPI',
      'paySign': paySign,
      success: function (res) {
        console.log(res)

        if (res.errMsg == 'requestPayment:ok') {

          wx.showToast({
            title: '支付成功',
          }),
            wx.navigateTo({
            url: '../blackbooksuccess/blackbooksuccess',
            })

        } else {

        }

      },
      'fail': function (res) {
        
      }
    })
  },
  
  userTelephone: function(e) {
    this.setData({
      userTelephone: e.detail.value
    })
  },
  useraddress: function (e) {
    this.setData({
      useraddress: e.detail.value
    })
  },
  userName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

})